import tkinter
import json
import time
import Operation
import admine_operation
import students_operation



#  总 标签
def label_title(win):
    label_load_username = tkinter.Label(win, text='用户名:', font=7)
    label_load_username.place(x=150, y=220)
    label_load_password = tkinter.Label(win, text='密码:', font=7)
    label_load_password.place(x=150, y=260)


def judge_loading_admine(username,password):
    b64_username = Operation.b64_str(username)
    b64_password = Operation.b64_str(password)
    with open(r'D:\admine.txt','r',encoding='utf8') as f:
        res = json.loads(f.read())
        if b64_username in res and res[b64_username] == b64_password:
            return '成功'
        else:
            return '失败'

def judge_loading_students(username,password):
    b64_username = Operation.b64_str(username)
    b64_password = Operation.b64_str(password)
    with open(r'D:\students.txt','r',encoding='utf8') as f:
        res = json.loads(f.read())
        if b64_username in res and res[b64_username] == b64_password:
            return '成功'
        else:
            return '失败'


def choose(r,entry1,entry2,win):
    get_choose = r.get()
    admine = Operation.get_text(entry1)
    admine_password = Operation.get_text(entry2)
    if get_choose == '1':
        reader = judge_loading_admine(admine,admine_password)
        if admine == '' or admine_password == '':
            label_tishi = tkinter.Label(win, text='不能为空！', font='楷体', fg='red')
            label_tishi.place(x=280, y=340)

        else:
            if reader == '成功':
                label_true = tkinter.Label(win,text = 'Loading - -  -- ---',fg = 'red')
                label_true.place(x=280,y=300)
                win.destroy()
                admine_operation.opertion()
            elif reader == '失败':
                label_tishi = tkinter.Label(win, text='登陆失败！', font='楷体', fg='red')
                label_tishi.place(x=280, y=340)

    elif get_choose == '2':
        reader = judge_loading_students(admine,admine_password)
        if admine == '' or admine_password == '':
            label_tishi = tkinter.Label(win, text='不能为空！', font='楷体', fg='red')
            label_tishi.place(x=280, y=340)
        else:
            if reader == '成功':
                label_true = tkinter.Label(win,text = 'Loading - -  -- ---',fg = 'green')
                label_true.place(x=280,y=300)
                win.destroy()
                students_operation.opertion()
            elif reader == '失败':
                label_tishi = tkinter.Label(win, text='登陆失败', font='楷体', fg='red')
                label_tishi.place(x=280, y=340)

    else:
        label_tishi = tkinter.Label(win, text='请进行选择', font='楷体', fg='red')
        label_tishi.place(x=280, y=340)




def new_window():

    window=tkinter.Toplevel()
    window.title('请登录__')
    window.geometry('800x600')

    # 开头
    font_show = ('楷体', 20)
    label_show = tkinter.Label(window, text='Welcome to 学生登录系统！', font=font_show)
    label_show.place(x=300)
    Label_show = tkinter.Label(window, text='欢迎登录学生系统!', font=30)
    Label_show.place(x=100, y=120)


    # 复选
    label_Checkbutton = tkinter.Label(window, text='Please  choose', font=('Arial', 12))
    label_Checkbutton.place(x=250, y=150)
    r = tkinter.StringVar()
    r.set(0)
    choose_admin = tkinter.Radiobutton(window, text='系统管理员', variable=r, value='1')
    choose_admin.place(x=220, y=180)
    choose_students = tkinter.Radiobutton(window, text='学生', variable=r, value='2')
    choose_students.place(x=320, y=180)

    # 总标签
    label_title(window)


#   entry 用户名 密码
    entry_username = tkinter.Entry(window,font=7,width=20)
    entry_username.place(x=220,y=220)
    entry_password = tkinter.Entry(window,text='密码',font=7,width=20,show = '*')
    entry_password.place(x=220,y=260)


    def load_tishi():
        choose(r, entry_username, entry_password, window)

    button_loading = tkinter.Button(window,text=' 安全登录 ',font='楷体',width=20,bg='skyblue',command=load_tishi)
    button_loading.place(x=220,y=300)

    window.mainloop()


